<template>
  <view>
    <view>res:</view>
    <view class="res">{{res}}</view>
    <button @click="res=''">clear</button>
    <button @click="uploadFile">File</button>
    <button @click="uploadBlob">Blob</button>
    <button @click="uploadBase64">Base64</button>
  </view>
</template>

<script>
export default {
  data () {
    return {
      res: ''
    }
  },
  methods: {
    uploadFile () {
      const file = new File(['test'], 'test.txt', { type: 'text/plain' })
      this.upload({ file })
    },
    uploadBlob () {
      const blob = new Blob(['test'], { type: 'text/plain' })
      // blob.name = 'test.txt'
      this.upload({ file: blob })
    },
    uploadBase64 () {
      const base64 = 'data:text/plain;base64,dGVzdAo='
      this.upload({ filePath: base64 })
    },
    upload (options) {
      uni.uploadFile(Object.assign({
        url: 'http://unidemo.dcloud.net.cn/upload',
        name: 'test-name',
        success: (res) => {
          console.log(res)
          this.res = JSON.stringify(JSON.parse(res.data), null, 2)
        },
        fail: (err) => {
          console.log(err)
        }
      }, options))
    }
  }
}
</script>

<style>
.res {
  white-space: pre;
  background-color: #ccc;
}
</style>